//class for find queries function, by Fransiska Putri Wina
#include <iostream>
#include <algorithm>
#include "Menu_Find_Queries.h"

using namespace std;

//default constructor
Menu_Find_Queries::Menu_Find_Queries() {}

//procedure for solving B2a problem, by Fransiska Putri Wina
void Menu_Find_Queries::B2a(vector<Order> orders,vector<Menu_Item> menu_items, vector<Order_Item> order_items) {
	 /* The following code has been written by Fransiska Putri Wina, solving problem B2a */
	 cout << "\nDisplay total sales for a given table\n\n";
	 int table;
	 int index = -1;
	 int i=0;
	 int len=19;
	 string str;
	 int orderid;
	 double sales = 0;
	 int itemid;
	 int quantity;
	 
	 cout << "Please input the table number:\n>> ";
	 cin >> table;
	 
	 for(int i = 0; i < orders.size(); i++) {
		if(orders[i].getTableID() == table) {
		   index = i;
		   break;
		}
	 }
	 
	 if(index==-1) {
		cout << "Apologies, there does not appear to be a table bearing that number.\n\n";
	 }
	 else {
		cout << "Sales for table  " << table << ": \n";
		cout << "------------------------------------------------------------" << endl;
		cout << "|Item Name          |Price              |Quantity           |" << endl;
		cout << "------------------------------------------------------------" << endl;
		orderid = orders[index].getOrderID();
		for (int i=0; i < order_items.size(); i++) {
		   if (order_items[i].getOrderID() == orderid) {
			  itemid = order_items[i].getMenuItemID();
			  quantity = order_items[i].getProdQty();
			  for (int j=0; j < menu_items.size(); j++) {
				 if (menu_items[j].menuItemId() ==  itemid) {
				 	
				 	cout << "|" << menu_items[j].get_menu_item_name();
					for (int k=0; k < len - menu_items[j].get_menu_item_name().size(); k++) {
						cout << " ";
					}
					cout << "|$";
					
				 	cout << menu_items[j].getPrice();
				 	//convert price to string
				 	ostringstream strs;
					strs << menu_items[j].getPrice();
					str = strs.str();
					for (int k=0; k < len - str.size()-1; k++) {
						cout << " ";
					}
					cout << "|";
					
					cout << quantity;
					//convert quantity to string
				 	ostringstream strs2;
					strs2 << quantity;
					str = strs2.str();
					for (int k=0; k < len - str.size(); k++) {
						cout << " ";
					}
					cout << "|" << endl;
				 	
				 
					 sales = sales + menu_items[j].getPrice() * quantity;
				 }
			  }
		   }
		}
		cout << "------------------------------------------------------------" << endl;
		cout << "                    |$" << sales;
		//convert quantity to string
		ostringstream strs3;
		strs3 << sales;
		str = strs3.str();
		for (int k=0; k < len - str.size()-1; k++) {
			cout << " ";
		}
		cout << "|" << endl << "                    ---------------------\n\n";
		//cout << "\nTotal sales for table  " << table << ": $" << sales << "\n\n";
	 }
	 /* End of code written by FPW */
}

//procedure for solving B2b problem, by Muhammad Ghufron Mahfudhi
void Menu_Find_Queries::B2b(vector<Order> orders,vector<Menu_Item> menu_items, vector<Order_Item> order_items) {
	 cout << "\nDisplay total sales for a given server\n\n";
	 int server;
	 vector<int> orderId;
	 cout << "Please input the server id:\n>> ";
	 cin >> server;
	 
	 //find order id from a given server id
	 for(int i = 0; i < orders.size(); i++)
	 {
		if(orders[i].getServerID() == server)
		{
		   orderId.push_back(orders[i].getOrderID());
		}
	 }
	 if(orderId.size() == 0)
	 {
	
		cout << "\nThere are no server: " << server << endl << endl << endl;
	 }
	 else
	 {
		//search the order items
		vector<Order_Item> orderan;
		for(int i = 0; i < orderId.size(); i++)
		{
		   for(int j = 0; j < order_items.size(); j++)
		   {
			  if(orderId[i] == order_items[j].getOrderID())
			  {
				 orderan.push_back(order_items[j]);
			  }
		   }
		}
		//search the menu items
		string menu[4][orderan.size()+1];
		double total = 0;
		menu[0][0] = " Order ID";
		menu[1][0] = " Menu Item";
		menu[2][0] = " Quantity";
		menu[3][0] = " Profit";
		for(int i = 0; i < orderan.size(); i++)
		{
		   int j = 0;
		   while(j < menu_items.size() && orderan[i].getMenuItemID() != menu_items[j].menuItemId())
		   {
			  j++;
		   }
		   if(j < menu_items.size())
		   {
			  stringstream ss1, ss2, ss3, ss4;
			  ss1 << " " << orderan[i].getOrderID();
			  menu[0][i+1] = ss1.str();
			  ss2 << " " << menu_items[j].get_menu_item_name() << " $" << menu_items[j].getPrice();
			  menu[1][i+1] = ss2.str();
			  ss3 << " " << orderan[i].getProdQty();
			  menu[2][i+1] = ss3.str();
			  ss4 << " $" << menu_items[j].getPrice() * orderan[i].getProdQty();
			  menu[3][i+1] = ss4.str();
			  total += (menu_items[j].getPrice() * orderan[i].getProdQty());
		   }
		}
	
		//print
		cout << "\nSales for server" << server << endl << endl;
		cout << "|";
		for(int i = 0; i < 10; i++)
		{
		   cout << "-";
		}
		for(int i = 0; i < 3; i++)
		{
		   cout << "|";
		   for(int j = 0; j < 20; j++)
		   {
			  cout << "-";
		   }
		}
		cout << "|\n";
		for(int i = 0; i < orderan.size()+1; i++)
		{
		   for(int j = 0; j < 4; j++)
		   {
			  if(j == 0)
			  {
				 cout << "|";
				 cout << menu[j][i];
				 int len = 10 - menu[j][i].size();
				 for(int k = 0; k < len; k++)
				 {
					cout << " ";
				 }
			  }
			  else
			  {
				 cout << "|";
				 cout << menu[j][i];
				 int len = 20 - menu[j][i].size();
				 for(int k = 0; k < len; k++)
				 {
					cout << " ";
				 }
			  }
		   }
		   cout << "|\n";
		   if(i == 0 || i == orderan.size())
		   {
			  cout << "|";
			  for(int j = 0; j < 10; j++)
			  {
				 cout << "-";
			  }
			  for(int j = 0; j < 3; j++)
			  {
				 cout << "|";
				 for(int k = 0; k < 20; k++)
				 {
					cout << "-";
				 }
			  }
			  cout << "|\n";
		   }
		   else
		   {
			  cout << "|";
			  for(int j = 0; j < 10; j++)
			  {
				 cout << " ";
			  }
			  for(int j = 0; j < 3; j++)
			  {
				 cout << "|";
				 for(int k = 0; k < 20; k++)
				 {
					cout << " ";
				 }
			  }
			  cout << "|\n";
		   }
		}
		cout << "| Total Cost";
		int len = 52 - 11;
		for(int i = 0; i < len; i++)
		{
		   cout << " ";
		}
		cout << "|";
		stringstream ss5;
		ss5 << " $" << total;
		string totalCost = ss5.str();
		cout << totalCost;
		len = 20 - totalCost.size();
		for(int i = 0; i < len; i++)
		{
		   cout << " ";
		}
		cout << "|\n";
		cout << "|";
		for(int i = 0; i < 73; i++)
		{
		   cout << "-";
		}
		cout << "|\n";
		cout << endl << endl;               
	 }
}

//procedure for solving B2c problem, by Muhammad Ghufron Mahfudhi
void Menu_Find_Queries::B2c(vector<Menu_Item> menu_items, vector<Order_Item> order_items) {
	 cout << "\nDisplay total sales for a given menu_item\n\n";
	 string menuName;
	 vector<Order_Item> orderItem;
	 cout << "Please input the menu item:\n>> ";
	 cin >> menuName;
	 
	 //find order id
	 int index = 0;
	 int menuId;
	 while(index < menu_items.size() && menu_items[index].get_menu_item_name() != menuName)
	 {
		index++;
	 }
	 if(index < menu_items.size())
	 {
		menuId = menu_items[index].get_menu_item_id();
		//find order id from a given menu item
		for(int i = 0; i < order_items.size(); i++)
		{
		   if(order_items[i].getMenuItemID() == menuId)
		   {
			  orderItem.push_back(order_items[i]);
		   }
		}
		if(orderItem.size() == 0)
		{
	
		   cout << "\nThere are no sales for menu item : " << menuName << endl << endl << endl;
		}
		else
		{
		   //search the menu items and price
		   string menu[4][orderItem.size()+1];
		   double total = 0;
		   menu[0][0] = " Order ID";
		   menu[1][0] = " Menu Item";
		   menu[2][0] = " Quantity";
		   menu[3][0] = " Cost";
		   for(int i = 0; i < orderItem.size(); i++)
		   {
			  int j = 0;
			  while(j < menu_items.size() && orderItem[i].getMenuItemID() != menu_items[j].menuItemId())
			  {
				 j++;
			  }
			  if(j < menu_items.size())
			  {
				 stringstream ss1, ss2, ss3, ss4;
				 ss1 << " " << orderItem[i].getOrderID();
				 menu[0][i+1] = ss1.str();
				 ss2 << " " << menu_items[j].get_menu_item_name() << " $" << menu_items[j].getPrice();
				 menu[1][i+1] = ss2.str();
				 ss3 << " " << orderItem[i].getProdQty();
				 menu[2][i+1] = ss3.str();
				 ss4 << " $" << menu_items[j].getPrice() * orderItem[i].getProdQty();
				 menu[3][i+1] = ss4.str();
				 total += (menu_items[j].getPrice() * orderItem[i].getProdQty());
			  }
		   }
	
		   //print
		   cout << "\nSales for menu " << menuName << endl << endl;
		   cout << "|";
		   for(int i = 0; i < 10; i++)
		   {
			  cout << "-";
		   }
		   for(int i = 0; i < 3; i++)
		   {
			  cout << "|";
			  for(int j = 0; j < 20; j++)
			  {
				 cout << "-";
			  }
		   }
		   cout << "|\n";
		   for(int i = 0; i < orderItem.size()+1; i++)
		   {
			  for(int j = 0; j < 4; j++)
			  {
				 if(j == 0)
				 {
					cout << "|";
					cout << menu[j][i];
					int len = 10 - menu[j][i].size();
					for(int k = 0; k < len; k++)
					{
					   cout << " ";
					}
				 }
				 else
				 {
					cout << "|";
					cout << menu[j][i];
					int len = 20 - menu[j][i].size();
					for(int k = 0; k < len; k++)
					{
					   cout << " ";
					}
				 }
			  }
			  cout << "|\n";
			  if(i == 0 || i == orderItem.size())
			  {
				 cout << "|";
				 for(int j = 0; j < 10; j++)
				 {
					cout << "-";
				 }
				 for(int j = 0; j < 3; j++)
				 {
					cout << "|";
					for(int k = 0; k < 20; k++)
					{
					   cout << "-";
					}
				 }
				 cout << "|\n";
			  }
			  else
			  {
				 cout << "|";
				 for(int j = 0; j < 10; j++)
				 {
					cout << " ";
				 }
				 for(int j = 0; j < 3; j++)
				 {
					cout << "|";
					for(int k = 0; k < 20; k++)
					{
					   cout << " ";
					}
				 }
				 cout << "|\n";
			  }
		   }
		   cout << "| Total Cost";
		   int len = 52 - 11;
		   for(int i = 0; i < len; i++)
		   {
			  cout << " ";
		   }
		   cout << "|";
		   stringstream ss5;
		   ss5 << " $" << total;
		   string totalCost = ss5.str();
		   cout << totalCost;
		   len = 20 - totalCost.size();
		   for(int i = 0; i < len; i++)
		   {
			  cout << " ";
		   }
		   cout << "|\n";
		   cout << "|";
		   for(int i = 0; i < 73; i++)
		   {
			  cout << "-";
		   }
		   cout << "|\n";
		   cout << endl << endl;               
		}
	 }
	 else
	 {
		cout << "\nThere are no menu item : " << menuName << endl << endl << endl;
	 }
	 
}

//procedure for solving C2c problem, by Muhammad Ghufron Mahfudhi
void Menu_Find_Queries::C2c(vector<Menu_Item> menu_items, vector<Order_Item> order_items) {
	 cout << "\nDisplay the top five selling menu items by revenue regardless of category\n\n";
	 vector<cost> costs;
	 for(int i = 0; i < menu_items.size(); i++)
	 {
		int quantity = 0;
		for(int j = 0; j < order_items.size(); j++)
		{
		   if(menu_items[i].get_menu_item_id() == order_items[j].getMenuItemID())
		   {
			  quantity += order_items[j].getProdQty();
		   }
		}
		double sum = quantity * menu_items[i].getPrice();
		cost kos(menu_items[i].get_menu_item_id(), menu_items[i].get_menu_item_name(), menu_items[i].getPrice(), quantity, sum);
		costs.push_back(kos);
	 }
	 sort(costs.begin(), costs.end());
	 string menu[4][6];
	 menu[0][0] = " Menu Item";
	 menu[1][0] = " Price";
	 menu[2][0] = " Quantity";
	 menu[3][0] = " Cost";
	 for(int i = 0; i < 5; i++)
	 {
		stringstream s1, s2, s3;
		menu[0][i+1] = " " + costs[i].menu_name;
		s1 << " $" << costs[i].price;
		menu[1][i+1] = s1.str();
		s2 << " " << costs[i].quantity;
		menu[2][i+1] = s2.str();
		s3 << " $" << costs[i].sum;
		menu[3][i+1] = s3.str();
	 }
	
	 //print
	 for(int i = 0; i < 4; i++)
	 {
		cout << "|";
		for(int j = 0; j < 18; j++)
		{
		   cout << "-";
		}
	 }
	 cout << "|\n";
	 for(int i = 0; i < 6; i++)
	 {
		for(int j = 0; j < 4; j++)
		{
		   cout << "|";
		   cout << menu[j][i];
		   int len = 18 - menu[j][i].size();
		   for(int k = 0; k < len; k++)
		   {
			  cout << " ";
		   }
		   
		}
		cout << "|\n";
		if(i == 0 || i == 5)
		{
		   for(int j = 0; j < 4; j++)
		   {
			  cout << "|";
			  for(int k = 0; k < 18; k++)
			  {
				 cout << "-";
			  }
		   }
		   cout << "|\n";
		}
		else
		{
		   for(int j = 0; j < 4; j++)
		   {
			  cout << "|";
			  for(int k = 0; k < 18; k++)
			  {
				 cout << " ";
			  }
		   }
		   cout << "|\n";
		}
		
	 }
	 cout << endl << endl;
}

//procedure for solving C2d problem, by Muhammad Ghufron Mahfudhi
void Menu_Find_Queries::C2d(vector<Order> orders,vector<Menu_Item> menu_items, vector<Order_Item> order_items) {
	 cout << "\nDisplay the order with the largest tab price\n\n";
	 int orderId;
	 double sum = 0;
	 int index = 0;
	 for(int i = 0; i < orders.size(); i++)
	 {
		double tot = 0;
		for(int j = 0; j < order_items.size(); j++)
		{
		   if(orders[i].getOrderID() == order_items[j].getOrderID())
		   {
			  int k = 0;
			  while (k < menu_items.size() && order_items[j].getMenuItemID() != menu_items[k].get_menu_item_id())
			  {
				 k++;
			  }
			  if(k < menu_items.size())
			  {
				 tot += (order_items[j].getProdQty() * menu_items[k].getPrice());
			  }
		   }
		}
		if(sum < tot)
		{
		   sum = tot;
		   orderId = orders[i].getOrderID();
		   index = i;
		}
	 }
	 vector<int> menuId;
	 vector<int> quantity;
	 for (int i = 0; i < order_items.size(); i++)
	 {
		if(order_items[i].getOrderID() == orderId)
		{
		   menuId.push_back(order_items[i].getMenuItemID());
		   quantity.push_back(order_items[i].getProdQty());
		}
	 }
	 if(menuId.size() > 0 && quantity.size() > 0)
	 {
		string menu[4][menuId.size()+1];
		menu[0][0] = " Menu Item";
		menu[1][0] = " Price";
		menu[2][0] = " Quantity";
		menu[3][0] = " Cost";
		for(int i = 0; i < menuId.size(); i++)
		{
		   stringstream s1, s2, s3;
		   int j = 0;
		   while(j < menu_items.size() && menu_items[j].get_menu_item_id() != menuId[i])
		   {
			  j++;
		   }
		   if(j < menu_items.size())
		   {
			  menu[0][i+1] = " " + menu_items[j].get_menu_item_name();
			  s1 << " $" << menu_items[j].getPrice();
			  menu[1][i+1] = s1.str();
			  s2 << " " << quantity[i];
			  menu[2][i+1] = s2.str();
			  s3 << " $" << (menu_items[j].getPrice() * quantity[i]);
			  menu[3][i+1] = s3.str();
		   }
		}
	
		//print
		cout << "Order : " << orderId << endl;
		Time timeOrder = orders[index].getOrderTime();
		Date dateOrder = orders[index].getOrderDate();
		cout << "Ordering Time : " << dateOrder.year() << "/" << dateOrder.month() << "/" << dateOrder.day() << " " << timeOrder.hour() << ":" << timeOrder.minute() << endl;
		
		for(int i = 0; i < 4; i++)
		{
		   cout << "|";
		   for(int j = 0; j < 18; j++)
		   {
			  cout << "-";
		   }
		}
		cout << "|\n";
		for(int i = 0; i < menuId.size()+1; i++)
		{
		   for(int j = 0; j < 4; j++)
		   {
			  cout << "|";
			  cout << menu[j][i];
			  int len = 18 - menu[j][i].size();
			  for(int k = 0; k < len; k++)
			  {
				 cout << " ";
			  }
		   
		   }
		   cout << "|\n";
		   if(i == 0 || i == menuId.size())
		   {
			  for(int j = 0; j < 4; j++)
			  {
				 cout << "|";
				 for(int k = 0; k < 18; k++)
				 {
					cout << "-";
				 }
			  }
			  cout << "|\n";
		   }
		   else
		   {
			  for(int j = 0; j < 4; j++)
			  {
				 cout << "|";
				 for(int k = 0; k < 18; k++)
				 {
					cout << " ";
				 }
			  }
			  cout << "|\n";
		   }            
		}
		cout << "|";
		cout << " Total Cost";
		for(int i = 0; i < 56-11; i++)
		{
		   cout << " ";
		}
		cout << "|";
		stringstream s4;
		s4 << " $" << sum;
		string totalCost = s4.str();
		cout << totalCost;
		for(int i = 0; i < 18 - totalCost.size(); i++)
		{
		   cout << " ";
		}
		cout << "|\n|";
		for(int i = 0; i < 75; i++)
		{
		   cout << "-";
		}
		cout << "|\n";
		cout << endl << endl;
	 }
         
}